<!-- 文章管理 -->
<template>
  <div style="padding-left:20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-cascader :options="typeList" v-model="data.type" size="medium" ref="update" @change="changeCascader" clearable :props="childrenList"></el-cascader>
        <!-- <el-select v-model="data.type" size="medium" class="iup_width200" placeholder="请选择类别">
          <el-option v-for="item in typeList" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="search">{{$t('lan.query')}}</el-button>
        <el-button type="info" size="medium" @click="reset">{{$t('lan.reset')}}</el-button>
        <el-button type="primary" size="medium" @click="add('create','')">{{$t('lan.add')}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="getList" v-loading="loading" :element-loading-text="$t('lan.dataLoading')" empty-text border stripe>
      <div slot="empty">
        <div v-if="loading"></div>
        <div v-else>{{$t('lan.available')}}</div>
      </div>
      <el-table-column type="index" :label="$t('lan.num')" align="center" width="140"></el-table-column>
      <el-table-column prop="title" :label="$t('lan.mainTitle')" align="center"></el-table-column>
      <el-table-column prop="subTitle" :label="$t('lan.subTitle')" align="center"></el-table-column>
      <el-table-column prop="picUrl" :label="$t('lan.video')" align="center">
        <template slot-scope="scope" v-if="scope.row.picUrl">
          <el-image class="viewer-img" v-if="scope.row.picUrl!='' && common.matchFileSuffixType(scope.row.picUrl,'image')" :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]"> -->
          </el-image>
          <video style="height:120px;width:200px" :src="scope.row.picUrl" v-if="scope.row.picUrl !='' && common.matchFileSuffixType(scope.row.picUrl,'video')" controls="controls">您的浏览器不支持视频播放</video>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="content" label="内容" align="center">
        <template slot-scope="scope">
          <span v-html="scope.row.content">{{scope.row.content}}</span>
        </template>
      </el-table-column> -->
      <el-table-column prop="typeName" :label="$t('lan.category')" align="center"></el-table-column>
      <el-table-column prop="addTime" :label="$t('lan.createTime')" :formatter="common.formatTime" align="center"></el-table-column>
      <el-table-column :label="$t('lan.operation')" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="add('edit',scope.row)">{{$t('lan.modify')}}</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)">{{$t('lan.del')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <pagination :total="total" :pageData="data" @hanleSize="changePage" @hanlePage="handleSizeChange"></pagination>
    <!-- 添加/删除 -->
    <productModal v-if="roleData.showModal" :roleData="roleData" @close="getRoleList" />
  </div>
</template>
<script>
import productModal from "component/permissions/productModal";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
export default {
  components: {
    productModal,
  },
  data () {
    return {
      getList: [],
      data: {
        pageSize: 10,
        currentPage: 1,
        type: "" // 查询条件
      },
      total: 0,
      loading: false,
      getTime: "",
      roleData: {},
      typeList: [], // 类别
      childrenList: {
        expandTrigger: 'click',
        children: 'subCategoryBOList',
        value: 'id',
        label: 'name'
      },
    };
  },
  methods: {
    changeCascader () {
      let data = this.$refs.update.getCheckedNodes(true)
      if (data != '') {
        this.data.type = data[0].data.id
      }
    },
    // 获取字典表
    async getterm () {
      const url = '/category/queryList'
      const result = await this.request.httpRequest('POST', url, { pid: 0 });
      if (result.code === 200) {
        this.typeList = result.data
      }
    },
    //点击页数触发
    changePage (page) {
      this.data.currentPage = page;
      this.getRoleList();
    },
    //选择页数最多显示几条数据
    handleSizeChange (val) {
      this.data.pageSize = val;
      this.getRoleList();
    },
    //查询
    changeInput () {
      this.data.currentPage = 1;
      this.getTime();
    },
    //查询关键字
    search () {
      this.data.currentPage = 1;
      this.getRoleList();
    },
    //重置列表
    reset () {
      Object.assign(this.$data.data, this.$options.data().data); //重置列表
      this.getRoleList();
    },
    //获取列表
    async getRoleList () {
      this.loading = true;
      let url = "/product/queryPage";
      let result = await this.request.httpRequest("POST", url, this.data);
      if (result.code == 200) {
        this.total = result.data.totalRows;
        this.data.pageSize = result.data.pageSize;
        this.data.currentPage = result.data.currentPage;
        this.getList = result.data.datas;
      } else {
        this.$message({ type: "error", message: result.msg });
      }
      this.loading = false;
    },
    //是否要删除
    del (row) {
      this.$confirm(this.$t('lan.delProduct'), this.$t('lan.prompt'), {
        confirmButtonText: this.$t('lan.confirm'),
        cancelButtonText: this.$t('lan.cancel'),
        type: "warning"
      }).then(async () => {
        this.loading = true;
        let url = `/product/delete/${row.id}`;
        let result = await this.request.httpRequest("DELETE", url);
        if (result.code == 200) {
          this.$message({ type: "success", message: result.msg });
          this.getRoleList();
        } else {
          this.loading = false;
          this.$message({ type: "error", message: result.msg });
        }
      });
    },
    //新增、修改
    add (type, row) {
      this.roleData = {
        title: type == "create" ? this.$t('lan.addItem') : this.$t('lan.modifyProduct'),
        showModal: true,
        roleId: row.id || "",
        type: type
      };
    }
  },
  mounted () {
    this.getterm();
    this.getRoleList();
    this.getTime = this.helper.debounce(this.getRoleList); //防抖
  },
  created () {
    document.onkeydown = e => {
      if (e.keyCode == 13) {
        this.search();
      }
    };
  }
};
</script>
<style scoped>
.el-form {
  padding: 15px 0px !important;
}
.el-form-item {
  margin-bottom: 0px !important;
}
</style>